package com.kh.first.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.kh.first.pojo.WfWxShareEntity;
import com.kh.first.utils.Tools;
import com.kh.first.utils.WxUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

//@Component
//@EnableScheduling
//@Async
public class AlarmTask {

    private static Logger log = LoggerFactory.getLogger(AlarmTask.class);
    public static String corpid = "ww2b07f038d04beb0f";
    // 第三方用户唯一凭证密钥
    public static String corpsecret = "fIjSqI_WBW7QqMJKSSrTb2WTUzkR0r6X3_4f1yanzSE";
    /**
     * 默认是fixedDelay 上一次执行完毕时间后执行下一轮
     */
    @Scheduled(
//            cron = "0 0 0/1 * * ?"
            fixedRate=3600000
    )
    public void accessToken() throws InterruptedException {
        WfWxShareEntity accessToken = WxUtil.getAccessToken(corpid, corpsecret);

        if (null != accessToken ) {
            log.info("获取access_token成功 token:{}", accessToken);
//            wxShareService.update(accessToken);
        }
    }
    public void getJsapiTicket() {

//        WfWxShareEntity accessToken = Tools.accessToken;

//        JSONObject jsonObj= FastJSONUtils.getJSONObject(accessToken.getContent());
//        String access_token = jsonObj.getString("access_token");
//
//        WfWxShareEntity jsapiTicket = WxUtil.getTicket(access_token);
//        if (null != jsapiTicket ) {
//            log.info("获取jsapiTicket成功，有效时长{}秒 token:{}", jsapiTicket.getContent());
//        }
//        wxShareService.update(jsapiTicket);
    }

    /**
     * fixedRate:上一次开始执行时间点之后5秒再执行
     */
//    @Scheduled(fixedRate = 5000)
    public void run1() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println(Thread.currentThread().getName() + "=====>>>>>使用fixedRate  {}" + (System.currentTimeMillis() / 1000));
    }

    /**
     * fixedDelay:上一次执行完毕时间点之后5秒再执行
     */
//    @Scheduled(fixedDelay = 5000)
    public void run2() throws InterruptedException {
        Thread.sleep(7000);
        System.out.println(Thread.currentThread().getName() + "=====>>>>>使用fixedDelay  {}" + (System.currentTimeMillis() / 1000));
    }

    /**
     * 第一次延迟2秒后执行，之后按fixedDelay的规则每5秒执行一次
     */
//    @Scheduled(initialDelay = 2000, fixedDelay = 5000)
    public void run3() {
        System.out.println(Thread.currentThread().getName() + "=====>>>>>使用initialDelay  {}" + (System.currentTimeMillis() / 1000));
    }
}
